const express = require('express')
const path = require('path')
const fs = require('fs').promises
const router = express.Router()
const {db} = require('../db/dbutils')

router.get('/china', async (req,res)=>{
    const datapath = path.join(__dirname,'../public/mapjsons/china.json')
    const jsondata = await fs.readFile(datapath,'utf8')
    const data = JSON.parse(jsondata)
    res.json(data)
})
router.get('/provinces/shandong', async (req,res)=>{
    const datapath = path.join(__dirname,'../public/mapjsons/provinces/shandong.json')
    const jsondata = await fs.readFile(datapath,'utf8')
    const data = JSON.parse(jsondata)
    res.json(data)
})
router.get('/list',(req,res)=>{
    db.all('select * from chinagdp10years',[],(err,rows)=>{
        res.send(rows)
    })
})
router.post('/add', (req,res)=>{
    let message = req.body
    console.log(message);
    // let time2num = new Date().getTime()
    // res.send(typeof time2num)

    db.run("insert into musicians ('name','namecn','nationality','mainworks','dateofbirth','dateofdeath') values(?,?,?,?,?,?)",[message.name,message.namecn,message.nationality,message.mainworks,message.dateofbirth,message.dateofdeath],(err,rows)=>{
        if (err) {
            res.send('后端add报错',err)
        } else {
            res.send('添加数据成功')
        }
    })
})
router.put('/modify', (req,res)=>{
    let message = req.body
    console.log(message);
    // let time2num = new Date().getTime()
    // res.send(typeof time2num)

    db.run("update musicians SET name=?,namecn=?,nationality=?,mainworks=?,dateofbirth=?,dateofdeath=? WHERE id=?",[message.name,message.namecn,message.nationality,message.mainworks,message.dateofbirth,message.dateofdeath,message.id],(err,rows)=>{
        if (err) {
            res.send('后端add报错',err)
        } else {
            res.send('修改数据成功')
        }
    })
})
router.delete('/delete/:id', (req,res)=>{
    let message = req.params
    console.log(message);
    // res.send(message)
    // let time2num = new Date().getTime()
    // res.send(typeof time2num)

    db.run("delete from musicians where id=?",[message.id],(err,rows)=>{
        if (err) {
            res.send(err)
        } else {
            res.send('添加数据成功')
        }
    })
})

router.get('/worklist/:mname',(req,res)=>{
    let message = req.params
    console.log('/worklist/:mname',message.mname);
    db.all('select * from musicianworks where mname=?',[message.mname],(err,rows)=>{
        console.log('/worklist/:mname',rows);
        res.send(rows)
    })
})
router.post('/workadd', (req,res)=>{
    let message = req.body
    console.log('/workadd',message);
    // let time2num = new Date().getTime()
    // res.send(typeof time2num)

    db.run("insert into musicianworks ('mname','wno','wname','wnamecn','createtime','wtype') values(?,?,?,?,?,?)",[message.mname,message.wno,message.wname,message.wnamecn,message.createtime,message.wtype],(err,rows)=>{
        if (err) {
            res.send('后端add报错',err)
        } else {
            res.send('添加数据成功')
        }
    })
})
router.get('/workinfo/:mname/:wname',(req,res)=>{
    // console.log('/workinfo',req.params);
    db.all('select * from musicianworks where mname=? and wname=?',[req.params.mname,req.params.wname],(err,rows)=>{
        console.log('/workinfo/:mname/:wname',rows);
        res.send(rows)
    })
})
router.put('/workmodify', (req,res)=>{
    let message = req.body
    console.log('/workmodify', message);
    // let time2num = new Date().getTime()
    // res.send(typeof time2num)

    db.run("update musicianworks SET mname=?,wno=?,wname=?,wnamecn=?,createtime=?,wtype=? WHERE id=?",[message.mname,message.wno,message.wname,message.wnamecn,message.createtime,message.wtype,message.id],(err,rows)=>{
        if (err) {
            res.send('后端add报错',err)
        } else {
            res.send('修改数据成功')
        }
    })
})
router.delete('/workdelete/:id', (req,res)=>{
    let message = req.params
    console.log('/workdelete/:id',message);
    // res.send(message)
    // let time2num = new Date().getTime()
    // res.send(typeof time2num)

    db.run("delete from musicianworks where id=?",[message.id],(err,rows)=>{
        if (err) {
            res.send(err)
        } else {
            res.send('添加数据成功')
        }
    })
})

module.exports = router